package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import beans.HistoryTask;
import dao.HistoryTaskDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetTaskState
 */
public class GetTaskState extends HttpServlet {
	private HistoryTaskDao historyTaskDao = new HistoryTaskDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String batchTaskHash = request.getParameter("taskHash");
		//2018-7-23添加，根据所有子任务的状态来判断批量任务的状态：finished、stopped、pause、running
		// 有一个暂停pause，整体暂停；
		// 有一个暂停stopped，整体暂停；
		// 有一个运行中running，整体运行中；
		// 全部完成finished，整体完成；
		int taskPauseNumber = historyTaskDao.getSubTaskStatement(batchTaskHash, "pause");
		int taskStoppedNumber = historyTaskDao.getSubTaskStatement(batchTaskHash, "stopped");
		int taskRunningNumber = historyTaskDao.getSubTaskStatement(batchTaskHash, "running");
		int taskFinishedNumber = historyTaskDao.getSubTaskStatement(batchTaskHash, "finished");
		int taskState = 4;
		if(taskPauseNumber>0){
			taskState = 3;
		}else if (taskStoppedNumber>0){
			taskState = 2;
		}else if(taskRunningNumber>0){
			taskState = 1;
		}

		HistoryTask historyTask = historyTaskDao.getHistoryTaskByTaskHash(batchTaskHash);
		JSONObject jsonObject = JSONObject.fromObject(historyTask.getJson());
		jsonObject.put("taskPauseNumber",taskPauseNumber);
		jsonObject.put("taskStoppedNumber",taskStoppedNumber);
		jsonObject.put("taskRunningNumber",taskRunningNumber);
		jsonObject.put("taskFinishedNumber",taskFinishedNumber);
		JSONArray state = historyTaskDao.getHistoryBatchTaskState(historyTask.getTask_hash());
		//10-30 UPDATE: 1:正在运行  2:错误返回  3 pend暂停、挂起    4:正确完成  : 6:其他
		JSONObject result = new JSONObject();
		result.put("taskState", taskState);
		result.put("taskName",historyTask.getName());
		result.put("json", jsonObject);
		result.put("state", state);
		result.put("orders",historyTask.getAlgorithmOrders());
		response.getWriter().write(result.toString());
		/*if (task == null) {
			result.put("taskName",historyTask.getName());
			result.put("json", historyTask.getJson());
			result.put("state", taskDao.getHistoryBatchTaskState(historyTask.getTask_hash()));
			result.put("orders",historyTask.getAlgorithmOrders());
			result.put("jobState",new JobDao().getBatchTaskPackedJobState(taskHash,true));
			result.put("subTaskHashes",taskDao.getSubTaskHashByBatchTaskHash(historyTask.getTask_hash()).getDatas());
			response.getWriter().write(result.toString());
		} else {
			if (task.isAlive()) {
				*//**
		 * return task state -1:not exist 0:waiting 1:running 2:pause
		 * 3:stop 4:finished
		 **//*
				if (task.runningTaskBean.isPause) {
					result.put("taskState", 2);
				} else if (task.runningTaskBean.isStop) {
					result.put("taskState", 3);
				} else {
					result.put("taskState", 1);
				}
				Map<String,AlgorithmThread> runningAlgorithms = task.runningTaskBean.runningAlgorithms;
				JSONArray blockState = new JSONArray();
				for (Map.Entry<String, AlgorithmThread> one : runningAlgorithms.entrySet()) {
					RunnableAlgorithm runnableAlgorithm = one.getValue().runnerableAlgorithm;
					JSONObject o = new JSONObject();
					o.put("blockId", runnableAlgorithm.getBlockId());
					o.put("state", 1);
					blockState.add(o);
				}
				for (Map.Entry<String,Object> one: task.runningTaskBean.runnableObjects.entrySet()) {
					JSONObject o = new JSONObject();
					beans.RunnableAlgorithm runnableAlgorithm = (beans.RunnableAlgorithm)one;
					o.put("blockId", runnableAlgorithm.getBlockId());
					o.put("state", 0);
					blockState.add(o);
				}
				result.put("blockState", blockState);
			} else {
				result.put("taskState", 3);
			}
			result.put("json", task.runningTaskBean.getJson());
			result.put("taskName",historyTask.getName());
			result.put("state", task.runningTaskBean.state.toString());
			result.put("orders",historyTask.getAlgorithmOrders());
			result.put("jobState",new JobDao().getBatchTaskPackedJobState(taskHash,true));
			result.put("subTaskHashes",taskDao.getSubTaskHashByBatchTaskHash(historyTask.getTask_hash()).getDatas());
			response.getWriter().write(result.toString());
		}*/
	}
}